package frpc.boot.centre;

import frpc.application.web.IRedisManagerController;
import frpc.boot.annotation.EnableRedisManager;
import frpc.boot.annotation.RedisAttribute;
import frpc.boot.manager.ICacheDescriptionManager;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.annotation.AnnotationAttributes;
import org.springframework.core.type.AnnotationMetadata;

import java.lang.reflect.Field;
import java.util.Map;

/**
 * 类描述: Redis管理接口注册器
 *
 * @author : 王雷
 * @date : 2022/6/22 0022 下午 7:02
 */
@Slf4j
@SuppressWarnings("unchecked")
//BeanDefinitionRegistryPostProcessor
public class RedisWebManagerRegistrar implements ImportBeanDefinitionRegistrar {

    @Override
    public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
        //这里获取通过@ControolerScan配置的路径信息
        AnnotationAttributes mapperScanAttrs = AnnotationAttributes
                .fromMap(importingClassMetadata.getAnnotationAttributes(EnableRedisManager.class.getName()));
        assert mapperScanAttrs != null;

        final Class<? extends IRedisManagerController> redisMessageControllerBean = mapperScanAttrs
                .getClass("redisMessageControllerBean");
        registerBean(registry, "redisMessageControllerBean", redisMessageControllerBean);

        final Class<? extends ICacheDescriptionManager> redisMessageManagerBean = mapperScanAttrs
                .getClass("redisMessageManagerBean");
        registerBean(registry, "redisMessageManagerBean", redisMessageManagerBean);

        //解析缓存缓存描述实体对象
        final Class<?>[] redisAttributeDescription = mapperScanAttrs.getClassArray("redisAttributeDescription");

        if (redisAttributeDescription.length > 0) {
            try {
                final Field cacheContainer = redisMessageManagerBean.getField("CACHE_CONTAINER");
                cacheContainer.setAccessible(true);
                final Map<String, String> cacheContainerMap = (Map<String, String>) cacheContainer.get(redisMessageManagerBean);
                log.debug("获取到的结果为：{}；类型为：{}", cacheContainer, cacheContainer.getType());
                //获取所有属性
                for (Class<?> aClass : redisAttributeDescription) {
                    final Field[] declaredFields = aClass.getDeclaredFields();
                    for (Field declaredField : declaredFields) {
                        final RedisAttribute annotation = declaredField.getAnnotation(RedisAttribute.class);
                        if (annotation != null) {
                            try {
                                declaredField.setAccessible(true);
                                final Object value = declaredField.get(aClass);
                                log.debug("属性：{}；值：{}；获取到的注解属性说明：{}", declaredField.getName(), value
                                        , annotation.attributeName());
                                if (value != null) {
                                    cacheContainerMap.put(value.toString(), annotation.attributeName());
                                }
                            } catch (Exception e) {
                                log.error("获取属性：{}；异常", declaredField.getName(), e);
                            }

                        }
                    }
                }
            } catch (Exception e) {
                log.error("获取静态对象异常", e);
            }
        }

//        registerInteractionObject();
    }

    /**
     * 功能描述：注册bean
     *
     * @date : 2022/6/22 0022 下午 7:17
     */
    private void registerBean(BeanDefinitionRegistry registry, String name, Class<?> beanClass) {
        RootBeanDefinition bean = new RootBeanDefinition(beanClass);
        registry.registerBeanDefinition(name, bean);
    }
}
